package tt.dz.biz.service.log;

import com.alibaba.fastjson.JSON;
import java.util.Date;
import javax.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import tt.dz.biz.annotation.MsLog;
import tt.dz.biz.dao.log.LogDAOImp;
import tt.dz.biz.service.common.CommonService;
import tt.dz.common.DzConstants;
import tt.dz.common.DzConstants.BizType;
import tt.dz.common.LogUtil;
import tt.dz.vo.LogVO;

@Aspect
@Component
public class LogService
  implements Ordered
{
  @Resource(name="commonService")
  private CommonService commonService;
  @Resource(name="logDAO")
  LogDAOImp logDAO;
  
  @Around(value="execution(* tt.dz.biz.service..*(..)) && @annotation(tt.dz.biz.annotation.MsLog)&& @annotation(msLog)", argNames="msLog")
  public Object around(ProceedingJoinPoint point, MsLog msLog)
    throws Throwable
  {
    Object result = null;
    Object args = point.getArgs();
    Object target = point.getTarget();
    Signature signature = point.getSignature();
    StringBuilder bizDescSB = new StringBuilder();
    LogVO logVo = new LogVO();
    Date startTime = this.commonService.getDBCurrentTime();
    DzConstants.BizType bizType = msLog.bizType();
    String bizCode = msLog.bizCode();
    String bizName = msLog.bizName();
    String bizDesc = msLog.bizDesc();
    Throwable bizException = null;
    try
    {
      result = point.proceed();
    }
    catch (Exception e)
    {
      bizDescSB.append(e.getMessage());
      bizException = e;
    }
    try
    {
      bizDescSB.append(bizDesc + ";");
      logVo.setStartTime(startTime);
      logVo.setBizCode(bizCode);
      logVo.setBizName(bizName);
      logVo.setBizType(Integer.valueOf(bizType.toString()));
      
      String _paramInJson = JSON.toJSONString(args);
      if (_paramInJson.length() > 200) {
        _paramInJson = _paramInJson.substring(0, 190) + "...";
      }
      logVo.setParamIn(_paramInJson);
      Date endTime = this.commonService.getDBCurrentTime();
      bizDescSB.append("执行类：" + target.getClass().getName() + ",方法：" + signature.getName());
      bizDescSB.append(" 耗时" + (endTime.getTime() - startTime.getTime()) + "毫秒。");
      
      String _paramOutJson = result != null ? JSON.toJSONString(result) : "";
      if (_paramOutJson.length() > 200) {
        _paramOutJson = _paramOutJson.substring(0, 190) + "...";
      }
      logVo.setParamOut(_paramOutJson);
      
      String _bizDesc = bizDescSB.toString();
      if (_bizDesc.length() > 200) {
        _bizDesc = _bizDesc.substring(0, 190) + "...";
      }
      logVo.setBizDesc(bizDescSB.toString());
      logVo.setEndTime(endTime);
      this.logDAO.log(logVo);
    }
    catch (Exception e)
    {
      LogUtil.error(e.getMessage(), e);
    }
    if (bizException != null) {
      throw bizException;
    }
    return result;
  }
  
  public int getOrder()
  {
    return 0;
  }
}
